/*
 * @Author: wangshiyang
 * @Date: 2023-04-23 09:43:02
 * @LastEditors: your name
 * @LastEditTime: 2024-01-27 09:47:37
 * @Description: 竖直飞线效果
 */
import * as Cesium from 'cesium';
class FlyLineMaterial {
    constructor(options) {
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._speed = undefined;
        this._percent = undefined;
        this.color = options.color;
        this.speed = options.speed;
        this.percent = options.percent;
    };

    get isConstant() {
        return false;
    }

    get definitionChanged() {
        return this._definitionChanged;
    }

    getType(time) {
        return Cesium.Material.LineFlowMaterialType;
    }

    getValue(time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }

        result.color = Cesium.Property.getValueOrDefault(this._color, time, Cesium.Color.RED, result.color);
        result.speed = Cesium.Property.getValueOrDefault(this._speed, time, 5.0, result.speed);
        result.percent = Cesium.Property.getValueOrDefault(this._percent, time, 0.1, result.percent);
        return result;
    }

    equals(other) {
        return (this === other ||
            (other instanceof FlyLineMaterial &&
                Cesium.Property.equals(this._color, other._color) &&
                Cesium.Property.equals(this._speed, other._speed) &&
                Cesium.Property.equals(this._percent, other._percent) &&
                Cesium.Property.equals(this._gradient, other._gradient))
        );
    }
}

Object.defineProperties(FlyLineMaterial.prototype, {
    color: Cesium.createPropertyDescriptor('color'),
    speed: Cesium.createPropertyDescriptor('speed'),
    percent: Cesium.createPropertyDescriptor('percent')
});

Cesium.Material.FlyLineMaterial = 'FlyLineMaterial';
Cesium.Material.LineFlowMaterialType = 'LineFlowMaterialType';
Cesium.Material.LineFlowMaterialSource =
    /*glsl*/`
    czm_material czm_getMaterial(czm_materialInput materialInput)
    {
        float ITime=czm_frameNumber*speed/1000.0;
        // 将范围打到(0,1)之间
        float t=fract(ITime);

        //  获取需要输出的纹理坐标
        czm_material material = czm_getDefaultMaterial(materialInput);
        vec2 st=materialInput.st;
        float alpha=0.;
        if(t-percent<=st.s && st.s<=t){
            // 让小段的飞线呈现渐变色
            alpha=(st.s-(t- percent))/percent;
        }

       //  取外部颜色的rgb分量
        material.diffuse = color.rgb;
        material.alpha=alpha;
        return material;
    }
    `;

Cesium.Material._materialCache.addMaterial(Cesium.Material.LineFlowMaterialType, {
    fabric: {
        type: Cesium.Material.LineFlowMaterialType,
        uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 1.0),
            speed: 10.0,
            percent: 0.1
        },
        source: Cesium.Material.LineFlowMaterialSource
    },
    translucent: function (material) {
        return true;
    }
});

export default FlyLineMaterial;
